var util = require("../../utils/utils.js")
Page({
  data: {
    phone:'',
    name:'',
    //学院/部门
    num:'',
    //学号
    math:'',
    dates: '请选择(请至少提前一天预约)',
    times: '请选择',


    startdates: '',
    starttimes: '',
    enddates: '',
    endtimes: '',

    limitdata:[],

    now:false,
    //毕业生
    graduate:'',
    //部门
    part:'',
    //预约列席
    type:'',
    //一、	所需证件及材料
    tip1text: [],
    //二、	办理流程
    tip2text: [],
    //图片
    imgimgpath:'',
  },
  onLoad:function(option){
    this.setData({
      part: option.part,
      graduate: option.graduate,
    })

    //

    const db = wx.cloud.database()
    db.collection("limits").where({
    }).get({
        success: res => {
          this.setData({
            limitdata: res.data,
          })
      }
    })
        // for(var i=0;i<res.data.length;i++){
        //   console.log(res.data[i]);
        //   if ("2019-11-13" == res.data[i].startdates){
        //     console.log("yes");
        //     if (res.data[i].starttimes < "09:00" && res.data[i].endtimes > "09:00"){
        //       console.log("yes1");
        //     }else{
        //       console.log("no1")
        //     }
        //   }else{
        //     console.log("no");
        //   }
        //   console.log(res.data[i].startdates);
        // }
    
    


    //用户根据预约类型进行提示材料和流程
    var typedb="bookingtype";
    if (option.type == "职工办理居住登记卡" || option.type =="在校学生办理居住登记卡"){
         typedb = "bookingcenter";
    }
    db.collection(typedb).where({
      type: option.type,
    }).get({
      success: res => {
        this.setData({
          type: res.data[0].type,
        })
        if (res.data[0].material.length > 0 && res.data[0].process.length > 0){
          this.setData({
            tip1text: res.data[0].material,
            tip2text: res.data[0].process,
          })
        }else{
          wx.cloud.downloadFile({
            fileID: 'cloud://schoolbooking-aad9e0.7363-schoolbooking-aad9e0/' + res.data[0].type+".jpg", // 文件 ID
            success: res => {
              // 返回临时文件路径
            
              this.setData({
                imgimgpath: res.tempFilePath,
              })
            },
            fail: console.error
          })
        }
        
      }, fail: err => {
        wx.showToast({
          icon: "none",
          title: '网络异常，请重试',
        })
      }
    })
   
    //获取当前管理员是否外出办公
    db.collection("status")
      .get({
        success: res => {
          this.setData({
            now: res.data[0]['now']
          })
        }, fail: err => {
          wx.showToast({
            icon: "none",
            title: '网络异常，请重试',
          })
        }
      })

  },
  //图片放大
  previewImg: function (e) {
    //console.log(e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: index,     //当前图片地址
      urls: [index],
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })

  },

  //  点击时间组件确定事件  
  bindTimeChange: function (e) {
    this.setData({
      times: e.detail.value
    })
  },
  //  点击日期组件确定事件  
  bindDateChange: function (e) {
    this.setData({
      dates: e.detail.value
    })
  },
  //姓名
  inputname:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  //学院或部门
  inputmath: function (e) {
    this.setData({
      math: e.detail.value
    })
  },
  //学院或部门
  inputnum: function (e) {
    this.setData({
      num: e.detail.value
    })
  },
  //手机号
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //提交预约
   tosumbit:function(){
     var that = this;
     //验证姓名
     if (that.data.name.length <1) {
       wx.showModal({
         title: '提示',
         content: "请输入姓名"
       })
       return
     }
     //验证学号
     if (that.data.graduate=="毕业生"){
       if (that.data.math.length < 1) {
         wx.showModal({
           title: '提示',
           content: "请输入学号"
         })
         return
       }
     }
     
     //验证学院和部门
     if (that.data.num.length <1) {
       wx.showModal({
         title: '提示',
         content: "请输入"+that.data.part
       })
       return
     }
     //验证手机号
     if (that.data.phone.length != 11) {
       wx.showModal({
         title: '提示',
         content: "请输入正确的手机号"
       })
       return
     }
     //验证日期
     
     if (that.data.dates.length !=10) {
       wx.showModal({
         title: '提示',
         content: "请选择日期"
       })
       return
     }
    //提前一天预约
     if (that.data.dates.length==10) {
       var t1 = that.data.dates;

       


       var timestamp = Date.parse(new Date());
       timestamp = timestamp / 1000;
       //获取当前时间  
       var n = timestamp * 1000;
       var date = new Date(n);
       //年  
       var Y = date.getFullYear();
       //月  
       var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
       //日  
       var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
       
       
       var t2 = Y+"-"+M+"-"+D
       
       //判断是否是周六日
       
       let time = util.formatDate(new Date());
       let date = util.getDates(1, t1);
       var flagday = date[0]['week'];
       console.log(flagday);
       if (flagday == "周六" || flagday == "周日") {
         wx.showModal({
           title: '提示',
           content: "请不要预约周六，周日这两天，谢谢"
         })
         return
       }



       t1 = t1.replace("-", "").replace("-", "")
       t2 = t2.replace("-", "").replace("-", "")
       //console.log(t1)
       //console.log(t2)
       if(t1>t2){
         //console.log("大于")
       }else{
         wx.showModal({
           title: '提示',
           content: "请至少提前一天预约"
         })
         return
       }
       
     }
     //验证时间
     if (that.data.times.length <4) {
       wx.showModal({
         title: '提示',
         content: "请选择时间"
       })
       return
     };

     var flag = true;
     for (var i = 0; i < that.data.limitdata.length; i++) {
       if (that.data.dates == that.data.limitdata[i].startdates) {
        if (that.data.limitdata[i].starttimes < that.data.times && that.data.limitdata[i].endtimes > that.data.times) {
          //在限制时间内
          wx.showModal({
            title: '提示',
            content: "该时间老师有事外出，请另换时间！"
          })
          flag = false;
          break;
        }
      }
    }
     //信息无误，进行提交
     const db = wx.cloud.database()//打开数据库连接
     if(flag){
      db.collection("booking").add({
        data: {
          type:that.data.type,
          phone: that.data.phone,
          name: that.data.name,
          num: that.data.num + " " + that.data.math,
          dates: that.data.dates,
          times: that.data.times,
          ischeck:0,
          isread:0,
          msg:'',
        }, success: res => {
          wx.showToast({
            title: '预约成功',
            duration: 6000,
            mask: true
          }),
          //start微信通知


          //end

          wx.switchTab({
            url: '../index/index',
          })
        }, fail: err => {
          wx.showToast({
            title: '预约失败，请重试',
          })
        }
      })
     }
   },
  
})